package com.damonserver.complexServer;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;

/**
 * Netty服务端启动类
 */
public class NettyServer {
    private static final int port = 8888; //设置服务端端口
    private static  EventLoopGroup group = new NioEventLoopGroup();//线程池
    //服务端用的是ServerBootstrap类，客户端用的是Bootstrap
    private static  ServerBootstrap bootstrap = new ServerBootstrap();

    public static void main(String[] args) throws InterruptedException {
        try {
            bootstrap.group(group);
            bootstrap.channel(NioServerSocketChannel.class);
            bootstrap.childHandler(new NettyServerInitializer());
            // 服务器绑定端口监听
            ChannelFuture f = bootstrap.bind(port).sync();
            System.out.println("服务端启动成功...");
            // 监听服务器关闭监听
            //这里关闭后，服务端并没有关闭运行
            f.channel().closeFuture().sync();
        } finally {
            group.shutdownGracefully(); //关闭EventLoopGroup，释放掉所有资源包括创建的线程
        }
    }
}

